

(function($){
	var ht = $(window).height();
	var wt = $(window).width();

	$.fn.f_popup = function(options){
		var defaults ={
			type:"alert",
			title:'提示',
			content:'请先登陆！',//object或字符串
			contentPosition:'left',//center,right
			popupStyle: null,
			closeButton:true,
			//default style:layer_main:230,110,20  
			size:{width:null, height:null, titleHeight:null},
			titleStyle:'be5', //black,be5标题栏的颜色
			verticalOffset:-50,//弹出框偏移程度
			horizontalOffset:0,
			overlayBackground:'#cdcdcd',//背景颜色
			overpayOpacity:0.3,//背景透明度
			overlayClick:false,
			okButton:{btnid:'btn_okid',btnclass:'btn_ok'},
			cancelButton:{btnid:'btn_cancelid',btnclass:'btn_cancel'},
			autoFocus: true,
			confirm: function(){ return false; },//false表示点击后关闭弹出框
			cancel: function(){ return false; },
			close: function(){},
			dh:$(document).height()
		};

		var opts = $.extend({},defaults,options);

		var pophtml = '<div class="layer_bg"><div class="layer_main blackBorder">'+
	        '<div class="popup_panel blackBg"><h1 class="l blackTxt">'+opts.title+
	        '</h1><button class="close btn_close4 r mt5 mr5" title="关闭"></button></div>' +
	        '<div class="popup_content"></div></div></div>';
	    var popup = $('<div />').addClass('od-popup').html(pophtml);

        function _reposition() {
			var top = ((ht / 2) - (popup.find('.layer_bg').height() / 2)) + opts.verticalOffset;
			var left = ((wt / 2) - (popup.find('.layer_bg').width() / 2)) + opts.horizontalOffset;
			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			// IE6 fix
			//if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();			
			popup.css({
				top: top + 'px',
				left: left + 'px'
			});
			$("#popup_overlay").height(opts.dh);
		};

		function _overlay(status) {
			switch( status ) {
				case 'show':
					_overlay('hide');
					$("BODY").append('<div id="popup_overlay"></div>');
					$("#popup_overlay").css({
						position: 'absolute',
						zIndex: 99998,
						top: '0px',
						left: '0px',
						width: '100%',
						height: opts.dh,
						background:opts.overlayBackground,
						opacity: opts.overpayOpacity
					}).click($.proxy(function(e){
						if(opts.overlayClick){
							__hide();
						}
					},this));
				break;
				case 'hide':
					$("#popup_overlay").remove();
				break;
			}
		};

		function alertButton(){
			var div=$('<div />');
			div.css({'text-align':'center','margin-top':'10px'});

			var btn =$('<button />').addClass('od-button').html('确 定');
			btn.attr('id',opts.okButton.btnid);
			btn.addClass(opts.okButton.btnclass);
			btn.appendTo(div);            
			popup.find('.popup_content').append(div); 

			popup.find('#'+opts.okButton.btnid).click($.proxy(function(e){
				if(!opts.confirm()) 
					_hide();
			},this));

			popup.find('#'+opts.okButton.btnid).keypress($.proxy(function(e){
				if( e.keyCode == 13 || e.keyCode == 27 ) 
					popup.find('#'+opts.okButton.btnid).trigger('click');
			},this));
			if(opts.autoFocus)
				popup.find('#'+opts.okButton.btnid).focus();
		};

		function isString(obj){
			type = {}; 
			toString = Object.prototype.toString;  
			return obj == null ? String( obj ) : type[ toString.call(obj) ] || "object"; 
		};
		function newHeight(){
			var h = popup.find('.layer_main').height()+10;
			popup.find('.layer_bg').css('height',h);      
		};

		function _show() {            
			_overlay('show');
			
			$("BODY").append(popup);
			// IE6 Fix
			//var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 		
			popup.css({
				position: 'fixed',
				zIndex: 99999,
				padding: 0,
				margin: 0
			});
			//set closeButton
			if(!opts.closeButton){
				popup.find('.close').hide();
			}

			//set width and height
			if(opts.size.width){
				popup.find('.layer_main').css({
					width:opts.size.width+'px'
				});
				popup.find('.layer_bg').css({
					width:opts.size.width+10+'px'
				});
				popup.find('.popup_panel').css({width:opts.size.width-10+'px'});
			};
			if(opts.size.height>=110){
				popup.find('.layer_main').css({
					height:opts.size.height+'px'
				});
				popup.find('.layer_bg').css({
					height:opts.size.height+10+'px'
				});
			}
			else if(opts.size.height&&opts.size.height<110){
				popup.find('.popup_content').children().css("padding","0px");
				popup.find('.popup_content').children().children().removeClass('mt10 ml10');
			};

			if(opts.size.titleHeight){
				popup.find('.popup_panel').css({height:opts.size.titleHeight+'px'});
			}

			_reposition();//弹出框定位

			if(opts.type == 'alert')
				alertButton();
			if(opts.type=="login")
				popup.find(".layer_main").addClass("login_content");
			//右键点击关闭无效
			popup.find('.close').on("contextmenu",function(){return false;});            

			popup.find('.close').click($.proxy(function(e){
				e.stopPropagation();
				_hide();
			},this));

			newHeight();
		};
		function _hide() {
			opts.close(true);
			popup.remove();
			_overlay('hide');
		};

		function setContent(){            
			if(isString(opts.content)){
				var p=$("<p style='padding: 10px 0' />").addClass("f6");
				var w=opts.size.width?opts.size.width:230;
				p.css({
					"width":w-20+"px",
					"text-align":opts.contentPosition
				});
				p.html(opts.content)
			}
			else{//为jquery object
				var p = opts.content;
			}
			popup.find('.popup_content').append(p);
		};
		return this.each(function(){
			if($('BODY').find(".od_popup").length==0){
				setContent();	
				_show();
			}			
		});

	}
})(jQuery)